<?php
namespace app\common\logic\home;
use app\common\model\MaterialModel;
use app\common\model\RecordModel;
use app\common\model\RelationStudentModel;
use app\common\model\MaterialClassModel;
use app\common\logic\Tools;

class Material
{
	/**
	 * [获取所有用户图片素材]
	 * @return [type] [description]
	 */
	public function getImgList(){
		$data = MaterialModel::where('type',2)->field('images')->select()->toArray();
		$images = [];
		foreach ($data as $key => $value) {
			$value =explode(',',$value['images']);
			foreach ($value as $k => $val) {
				if(!empty($val)){
					$images[] = $val;
				}
			}
		}
		return $images;
	}
	/**
	 * [获取某一用户的七牛云素材]
	 * @return [type] [description]
	 */
	public function getUserList($param,$page=20){

		$data = MaterialModel::where(function($query) use($param){
								if(!empty($param['user_id'])){
									$query->where('userId',$param['user_id']);
								}
								if(!empty($param['ids'])){
									$query->where('id','in',$param['ids']);
								}
							})->where(function($query){
								$query->whereOr('images','<>','')->whereOr('video','<>','')->whereOr('audio','<>','')->whereOr('voice','<>','');
							})->field('images,video,audio,voice,id,memory,type')->paginate($page,false,['query'=>$param])->each(function($value,$item){
								
								
								$data = [];
								$type = [];
								if($value['images'] !=''){
									$data_images = explode(',', $value['images']);
									foreach ($data_images as $key => $val) {
										if(!empty($val)){
											$data[] = $val;
										}
										if($value['type'] == 2){
											$value['memory'] += 130;
										}
									}
									$type[] = 'jpg';
					            }
					            
					            if($value['video'] !=''){

					                $data_videos = explode(',', $value['video']);

					                foreach ($data_videos as $key => $val) {

										if(!empty($val)){
											$data[] = $val;
										}
									}
									$type[] = 'mp4';
					            }
					            if($value['audio'] !=''){
					            	$data_audios = explode(',', $value['audio']);
					            	foreach ($data_audios as $key => $val) {
										if(!empty($val)){
											$data[] = $val;
										}
									}
									$type[] = 'mp3';
					                
					            }
					            if($value['voice'] !=''){
					            	$data_voices = explode(',', $value['voice']);
					            	foreach ($data_voices as $key => $val) {
										if(!empty($val)){
											$data[] = $val;
										}
									}
					               	$type[] = 'mp3';
					            }
					          
					            $value['data'] = $data;
					            $value['type'] = implode(',',$type);
					            $value['memory'] = round($value['memory']/1024,2);
						    });
		return $data;
	}
	/**
	 * 获取用户素材
	 * @return [type] [description]
	 */
	public function getList($user_id,$param){
        //时间搜索
        //$startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        //$endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time'].'+1 month');
		$data = MaterialModel::where('userId',$user_id)
					->where('show_status',1)
					->where(function($query) use($param){
						if(!empty($param['start_time'])){
							$query->where('createTime','>=',$param['start_time']);
						}
						if(!empty($param['end_time'])){
							$query->where('createTime','<=',$param['end_time']);
						}
					})
					->where('delete_time',null)
					->order('id desc')
					->paginate(20,false,['query'=>$param])->each(function($value,$item){
						if($value['images'] !=''){
							$value['imgs'] = explode(',', $value['images']);
			                //获取缩略图
			                $imgs = [];
			                foreach($value['imgs'] as $k=>$val){
			                    if(!empty($val)){
			                        $rs = Tools::getHttp($val.'-thumbnail300x300');
			                        if(strlen($rs) > 30){
			                            $imgs[$k] = $val.'-thumbnail300x300';
			                        }else{
			                            $imgs[$k] = $val;
			                        }  
			                    }              
			                }
			                $value['imgs'] = $imgs;
			  
			            }
			            if($value['video'] !=''){
			                $value['videos'] = explode(',', $value['video']);
			            }
			            if($value['audio'] !=''){
			                $value['audios'] = explode(',', $value['audio']);
			            }
			            if($value['voice'] !=''){
			                $value['voices'] = explode(',', $value['voice']);
			            }
			            if(!empty($value['rec_time'])){
			                $value['rec_time'] = explode(',', $value['rec_time']);
			            }else{
			                $value['rec_time'] = '';
			            }
			          
			            $date = substr($value['createTime'], 0,10);
			            $time = substr($value['createTime'], -8);

			            $city = mb_substr($value['city'],0, mb_strlen($value['city'])-1);
			           // var_dump($date);
			            $value['date'] = explode('-',$date);
			            $value['time'] = explode(':',$time);
			            $value['city'] = $city;
			            if($value['city'] !=''){
			            	$value['adr'] = $city." ".$value['address'];
			            }else{
			            	$value['adr'] = $value['address'];
			            }
					});
       	return $data;
	}
	/**
	 * [同一组成员]
	 * @param  [type] $id      [description]
	 * @param  [type] $user_id [description]
	 * @return [type]          [description]
	 */
	public function group($id,$user_id){
		$class_id = RecordModel::where('s_user_id',$user_id)->where('status',3)->where('mytask_id',$id)->value('class_id');
		$data = NULL;
		if(!empty($class_id)){
			$data = RelationStudentModel::relation('appuser')->where('class_id',$class_id)->field('user_id,level')->select()->toArray();
		}
		return $data;
		
	}
	
	/**
	 * [设置某素材的同组成员]
	 * @param [type] $material_id [素材ID]
	 * @param [type] $id          [课题ID]
	 * @param [type] $user_id     [用户ID]
	 */
	public function setMaterial($material_id,$id,$user_id){
		$class_id = RecordModel::where('s_user_id',$user_id)->where('status',3)->where('mytask_id',$id)->value('class_id');//班组ID

		if(empty($class_id)){
			return 0;
		}
		$data_material_class = MaterialClassModel::where('class_id',$class_id)->where('material_id',$material_id)->field('material_class_id,status')->find();//查找是否有记录
		if(empty($data_material_class)){
			$data_material_class = [
					'class_id' => $class_id,
					'user_id' => $user_id,
					'material_id' => $material_id,
					'mytask_id' => $id,
					'status' => 2,
				];
			$MaterialClassModel = new MaterialClassModel();
			$rs = $MaterialClassModel->save($data_material_class);
		}else{
			$rs = MaterialClassModel::where('material_class_id',$data_material_class['material_class_id'])->delete();
		}
		return $rs;

	}
	/**
	 * [设置某素材的同组成员]
	 * @param [type] $material_id [素材ID]
	 * @param [type] $id          [课题ID]
	 * @param [type] $user_id     [用户ID]
	 */
	public function setMaterial1111($material_id,$id,$user_id){
		$class_id = RecordModel::where('s_user_id',$user_id)->where('status',3)->where('mytask_id',$id)->value('class_id');//班组ID
		$data_material_class = MaterialClassModel::where('class_id',$class_id)->where('material_id',$material_id)->field('material_class_id,status')->find();//查找是否有记录
		if(empty($data_material_class)){
			$data_material_class = [
					'class_id' => $class_id,
					'user_id' => $user_id,
					'material_id' => $material_id,
					'status' => 2,
				];
			$MaterialClassModel = new MaterialClassModel();
			$rs = $MaterialClassModel->save($data_material_class);
		}else{
			if($data_material_class['status'] == 1){
				$rs = MaterialClassModel::where('material_class_id',$data_material_class['material_class_id'])->update(['status'=>2]);
			}else{
				$rs = MaterialClassModel::where('material_class_id',$data_material_class['material_class_id'])->update(['status'=>1]);
			}
		}
		return $rs;

	}


	public function getLists11111($user_ids,$param){

		$data = MaterialModel::where('userId','in',$user_ids)
					->where(function($query) use($param){
						switch ($param['type']) {
							case 0:
								$query->where('content','<>',null)->where('images',null);
								break;
							case 1:
								$query->where('images','<>',null);
								break;
							case 2:
								$query->where('video','<>',null);
								break;
							case 3:
								$query->whereOr('format',3)->whereOr('format',4);
								break;
							default:
								# code...
								break;
						}
					})
					->where('delete_time',null)
					->order('createTime desc')
					->paginate(20,false,['query'=>$param])
					->each(function($value,$item){
						if($value['images'] !=''){
			                $value['imgs'] = explode(',', $value['images']);
			            }
			            if($value['video'] !=''){
			                $value['videos'] = explode(',', $value['video']);
			            }
			            if($value['audio'] !=''){
			                $value['audios'] = explode(',', $value['audio']);
			            }
			            if($value['voice'] !=''){
			                $value['voices'] = explode(',', $value['voice']);
			            }
			            if(!empty($value['rec_time'])){
			                $value['rec_time'] = explode(',', $value['rec_time']);
			            }else{
			                $value['rec_time'] = '';
			            }
			          
			            $date = substr($value['createTime'], 0,10);
			            $time = substr($value['createTime'], -8);

			            $city = mb_substr($value['city'],0, mb_strlen($value['city'])-1);
			           // var_dump($date);
			            $value['date'] = explode('-',$date);
			            $value['time'] = explode(':',$time);
			            $value['city'] = $city;
			            if($value['city'] !=''){
			            	$value['adr'] = $city." ".$value['address'];
			            }else{
			            	$value['adr'] = $value['address'];
			            }
					});
      	return $data;
	}

    
}
